package com.milotnt.controller;

import com.milotnt.pojo.ClassOrder;
import com.milotnt.pojo.ClassTable;
import com.milotnt.pojo.Coach;
import com.milotnt.service.ClassOrderService;
import com.milotnt.service.ClassTableService;
import com.milotnt.service.CoachService;
import com.milotnt.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/coach")
public class CoachController {

    @Autowired
    private CoachService coachService;

    @Autowired
    private RandomUtil randomUtil;

    @Autowired
    private ClassTableService tableService;

    @Autowired
    private ClassOrderService orderService;

    // 查询所有教练
    @RequestMapping("/selCoach")
    public String selCoach(Model model, HttpSession session){
        List<Coach> coachList = coachService.findAll();
        model.addAttribute("coachList", coachList);
        Object msg = session.getAttribute("msg");
        if(msg != null){
            model.addAttribute("msg", msg);
            session.removeAttribute("msg");
        }
        return "selectCoach";
    }

    // 跳转到新增教练页面
    @RequestMapping("/toAddCoach")
    public String toAddCoach(){
        return "addCoach";
    }

    // 新增教练
    @RequestMapping("/addCoach")
    public String addCoach(Coach coach){
        //教练账号
        Integer id = randomUtil.getAccount();
        //初始密码
        String password = "123456";
        //入职时间
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = simpleDateFormat.format(date);
        // 赋值
        coach.setCoachPassword(password);
        coach.setCoachId(id);
        coach.setEntryTime(nowDay);
        coachService.insertCoach(coach);
        return "redirect:selCoach";
    }

    // 跳转到修改教练信息页面
    @RequestMapping("/toUpdateCoach")
    public String toUpdateCoach(Integer coachId, Model model){
        Coach coach = coachService.selectByCoachId(coachId);
        model.addAttribute("coach", coach);
        return "updateCoach";
    }

    // 修改教练信息
    @RequestMapping("/updateCoach")
    public String updateCoach(Coach coach){
        coachService.updateCoach(coach);
        return "redirect:selCoach";
    }

    // 解雇教练
    @RequestMapping("/delCoach")
    public String delCoach(Integer coachId, HttpSession session){
        String s = coachService.deleteByCoachId(coachId);
        if(s != null){
            session.setAttribute("msg", s);
        }
        return "redirect:selCoach";
    }

    //跳转个人信息页面
    @RequestMapping("/toCoachInfo")
    public String toUserInformation(Model model, HttpSession session) {
        Coach coach = (Coach) session.getAttribute("coach");
        model.addAttribute("coach", coach);
        return "coachInformation";
    }

    //跳转修改个人信息页面
    @RequestMapping("/toUpdateInfo")
    public String toUpdateUserInformation(HttpSession session, Model model) {
        Coach coach = (Coach) session.getAttribute("coach");
        model.addAttribute("coach", coach);
        return "updateCoachInformation";
    }

    //修改个人信息
    @RequestMapping("/updateInfo")
    public String updateUserInformation(HttpSession session, Coach coach) {
        Coach coach1 = (Coach) session.getAttribute("coach");

        coach.setCoachId(coach1.getCoachId());
        coach.setCoachSlave(coach1.getCoachSlave());
        coach.setEntryTime(coach1.getEntryTime());

        coachService.updateCoach(coach);
        session.setAttribute("coach", coach);
        return "coachInformation";
    }

    // 查询当前教练教授的课程
    @RequestMapping("/toTeachClass")
    public String toTeachClass(Model model, HttpSession session){
        Coach coach = (Coach) session.getAttribute("coach");
        Integer coachId = coach.getCoachId();
        List<ClassTable> classList = tableService.findAllByCoachId(coachId);
        model.addAttribute("classList", classList);
        return "teachClass";
    }

    // 查询课程的报名信息
    @RequestMapping("/selClassOrder")
    public String selClassOrder(Integer classId, Model model){
        List<ClassOrder> classOrderList = orderService.selectMemberOrderList(classId);
        model.addAttribute("classOrderList", classOrderList);
        return "selectCoachOrder";
    }
}
